<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html >
<html>
<head>
    <base href="${base}/"/>
    <title>后台管理 - 商品列表</title>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="css/admin.css"/>
    <style>
        /* 商品列表样式 */
        .list_table {
            width: 100%;
            overflow-x: auto;
        }

        .list_table table {
            width: 100%;
            border-collapse: collapse;
        }

        .list_table th, .list_table td {
            padding: 10px;
            text-align: center;
            border: 1px solid #ddd;
        }

        .list_table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }

        .list_table tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .list_table tr:hover {
            background-color: #f1f1f1;
        }

        /* 操作按钮样式 */
        .btn_increase, .btn_decrease, .btn_delete {
            display: inline-block;
            padding: 5px 10px;
            margin: 0 3px;
            border-radius: 3px;
            text-decoration: none;
            font-size: 12px;
            cursor: pointer;
        }

        .btn_increase {
            background-color: #28a745;
            color: #fff;
        }

        .btn_decrease {
            background-color: #007bff;
            color: #fff;
        }

        .btn_delete {
            background-color: #dc3545;
            color: #fff;
        }

        /* 数量输入框样式 */
        .quantity_input {
            width: 50px;
            text-align: center;
        }
        /* 分页导航样式 */
        .pagination {
            margin-top: 20px;
            text-align: center;
        }

        .pagination a, .pagination span {
            display: inline-block;
            padding: 8px 12px;
            margin: 0 5px;
            border: 1px solid #ddd;
            text-decoration: none;
            color: #333;
            border-radius: 3px;
        }

        .pagination a:hover {
            background-color: #f5f5f5;
        }

        .pagination .current {
            background-color: #007bff;
            color: #fff;
            border-color: #007bff;
        }

        .pagination .disabled {
            color: #999;
            border-color: #eee;
            cursor: not-allowed;
        }

        .pagination .disabled:hover {
            background-color: transparent;
        }

        /* 已删除商品样式 (模仿管理员列表) */
        .deleted-goods {
            color: gray;
            font-style: italic;
        }
    </style>
    <script type="text/javascript" src="js/jquery-3.7.1.min.js"></script>
    <script type="text/javascript">
        function increaseQuantity(goodsId) {
            var quantityInput = $('#quantity_' + goodsId);
            var currentQuantity = parseInt(quantityInput.val());
            var newQuantity = currentQuantity + 1;
            quantityInput.val(newQuantity);
            $.post('/admin/goods/updateStock', { id: goodsId, quantity: newQuantity }, function(response) {
                if (response.success) {
                    var row = quantityInput.closest('tr');
                    row.removeClass('deleted-goods');
                    row.find('td:nth-child(2)').html(row.find('td:nth-child(2)').text().replace('（已删除）', ''));
                    row.find('.btn_decrease').prop('disabled', false);
                } else {
                    alert('更新库存失败：' + response.message);
                    quantityInput.val(currentQuantity); // 回滚
                }
            }, 'json');
        }

        function decreaseQuantity(goodsId) {
            var quantityInput = $('#quantity_' + goodsId);
            var currentQuantity = parseInt(quantityInput.val());
            if (currentQuantity > 0) {
                var newQuantity = currentQuantity - 1;
                quantityInput.val(newQuantity);
                $.post('/admin/goods/updateStock', { id: goodsId, quantity: newQuantity }, function(response) {
                    if (response.success) {
                        var row = quantityInput.closest('tr');
                        if (newQuantity === 0) {
                            row.addClass('deleted-goods');
                            row.find('td:nth-child(2)').html(row.find('td:nth-child(2)').text() + '（已删除）');
                            row.find('.btn_decrease').prop('disabled', true);
                        } else if (currentQuantity === 0) {
                            row.removeClass('deleted-goods');
                            row.find('td:nth-child(2)').html(row.find('td:nth-child(2)').text().replace('（已删除）', ''));
                            row.find('.btn_decrease').prop('disabled', false);
                        }
                    } else {
                        alert('更新库存失败：' + response.message);
                        quantityInput.val(currentQuantity); // 回滚
                    }
                }, 'json');
            }
        }

        function deleteGoods(goodsId) {
            if (confirm('确定要删除该商品吗？')) {
                window.location.href = '/admin/goods/delete?id=' + goodsId;
            }
        }
    </script>
</head>
<body>
<div class="container">
    <div id="header">
        <div class="logo">
            <a href=""><img src="images/admin/logo.png" width="303" height="43" /></a>
        </div>
        <p>
            <a href="/admin/logout">退出管理</a> <a href="/admin">后台首页</a> <a href="/" target='_blank'>商城首页</a>
            <span>您好 <label class='bold'>${sessionScope.manager.account}</label></span>
        </p>
    </div>

    <div id="admin_left">
        <ul class="submenu">
            <ul class="menu">
                <li><a href="/admin/goods/list">商品列表</a></li>
                <li><a href="admin/goods/add">增加商品</a></li>
                <li><a href="admin/user/list">用户列表</a></li>
                <li><a href="admin/manager/list">管理员列表</a></li>
                <li><a href="/admin/manager/add">添加管理员</a></li>
            </ul>
        </ul>
        <div id="copyright"></div>
    </div>

    <div id="admin_right">
        <div class="content_box">
            <div class="content">
                <div class="title">
                    <h2>商品列表</h2>
                </div>
                <div class="list_table">
                    <table cellpadding="0" cellspacing="0">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>分类ID</th>
                            <th>货号</th>
                            <th>市场价</th>
                            <th>销售价</th>
                            <th>库存</th>
                            <th>图片</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="goodsTableBody">
                        <c:forEach items="${goodsPages.records}" var="goods">
                            <tr class="${goods.stock == 0 ? 'deleted-goods' : ''}">
                                <td>${goods.id}</td>
                                <td class="${goods.stock == 0 ? 'deleted-goods' : ''}">${goods.name} <c:if test="${goods.stock == 0}">（已删除）</c:if></td>
                                <td>${goods.categoryId}</td>
                                <td>${goods.no}</td>
                                <td>${goods.marketPrice}</td>
                                <td>${goods.salePrice}</td>
                                <td>
                                    <button class="btn_decrease" onclick="decreaseQuantity(${goods.id})"
                                            <c:if test="${goods.stock == 0}">disabled</c:if>>-</button>
                                    <input type="text" id="quantity_${goods.id}" class="quantity_input" value="${goods.stock}" readonly>
                                    <button class="btn_increase" onclick="increaseQuantity(${goods.id})">+</button>
                                </td>
                                <td><img src="${goods.image}" alt="${goods.name}" width="50"></td>
                                <td>
                                    <a href="javascript:void(0);" onclick="deleteGoods(${goods.id})" class="btn_delete">删除</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>

                <div class="pagination">
                    <c:if test="${currentPages > 1}">
                        <a href="/admin/goods/list?adminPage=${currentPages - 1}&pagesSize=${goodsPages.getSize()}">上一页</a>
                    </c:if>
                    <c:forEach var="i" begin="1" end="${totalPagess}">
                        <c:choose>
                            <c:when test="${currentPages == i}">
                                <span class="current">${i}</span>
                            </c:when>
                            <c:otherwise>
                                <a href="/admin/goods/list?adminPage=${i}&pagesSize=${goodsPages.getSize()}">${i}</a>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
                    <c:if test="${currentPages < totalPagess}">
                        <a href="/admin/goods/list?adminPage=${currentPages + 1}&pagesSize=${goodsPages.getSize()}">下一页</a>
                    </c:if>
                    <span>共 ${goodsPages.total} 条记录，${totalPagess} 页</span>
                </div>
            </div>
        </div>
    </div>
    <div id="separator"></div>
</div>
</body>
</html>